//
//  KAApplyIdAlert.swift
//  kano
//
//  Created by Frank on 2024/12/19.
//


import UIKit

class KAApplyIdAlert: UIView {
        
    lazy var var_backgroundView: UIView = {
        let var_backgroundView = UIVisualEffectView(frame: bounds)
        var_backgroundView.frame = bounds
        var_backgroundView.backgroundColor = UIColor.ka_hexColor(0x000000, alphaVaule: 0.66)
        return var_backgroundView
    }()
    
    lazy var var_contentView: UIView = {
        let var_contentView = UIView()
        var_contentView.frame = var_backgroundView.bounds
        var_contentView.backgroundColor = .clear//.color66748A
        return var_contentView
    }()
    
    
    lazy var var_cornerView: UIView = {
        let var_cornerView = UIView()
        var_cornerView.backgroundColor = .white
        return var_cornerView
    }()
    
    
    lazy var var_titleLabel: UILabel = {
        let var_titleLabel = UILabel()
        var_titleLabel.text = ""
        var_titleLabel.textColor = .black
        var_titleLabel.font = kFontSize(14)
        var_titleLabel.numberOfLines = 0
        var_titleLabel.textAlignment = .left
        var_titleLabel.translatesAutoresizingMaskIntoConstraints = false
        return var_titleLabel
    }()
    
    lazy var var_messageLabel: UILabel = {
        let var_messageLabel = UILabel()
        var_messageLabel.text = ""
        var_messageLabel.textColor = .black
        var_messageLabel.font = kFontSize(14)
        var_messageLabel.textAlignment = .left
        var_messageLabel.numberOfLines = 0
        var_messageLabel.translatesAutoresizingMaskIntoConstraints = false
        return var_messageLabel
    }()
    
    lazy var var_switchBtn: UIButton = {
        let var_switchBtn = UIButton()
        var_switchBtn.backgroundColor = .themeColor
        var_switchBtn.setTitleColor(.white, for: .normal)
        var_switchBtn.setTitle("Switch accounts", for: .normal)
        var_switchBtn.titleLabel?.font = kFont(16)
        var_switchBtn.addTarget(self, action: #selector(ka_switchBtnClick), for: .touchUpInside)
        return var_switchBtn
    }()
    
    @objc func ka_switchBtnClick() {
        KAUserManager.shared.userLogout()
        KAVCManager.switchRootVC(false)
    }
    
    lazy var var_closeBtn: UIButton = {
        let var_closeBtn = UIButton()
        var_closeBtn.backgroundColor = .white
        var_closeBtn.setTitleColor(.black, for: .normal)
        var_closeBtn.setTitle("Close", for: .normal)
        var_closeBtn.titleLabel?.font = kFont(16)
        var_closeBtn.addTarget(self, action: #selector(ka_closeButtonClick), for: .touchUpInside)
        return var_closeBtn
    }()
    
    @objc func ka_closeButtonClick() {
        self.ka_dismiss()
    }
    
}

extension KAApplyIdAlert {
    @objc func ka_show() {
        alpha = 1.0
        let var_rootWindow : UIWindow = UIApplication.shared.keyWindow!
        var_rootWindow.addSubview(self)
        var_backgroundView.alpha = 0

        UIView.animate(withDuration: 0.25, delay: 0.0, animations: { [self] in
            var_backgroundView.alpha = 1
        })
    }
    
    @objc func ka_dismiss() {
        endEditing(true)
        UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveLinear, animations: { [self] in

        }, completion: { [self] _ in
            alpha = 0
            removeFromSuperview()
        })
    }
    
    convenience init(title: String, subTitle: String) {
        self.init(frame: CGRect(x: 0, y: 0, width: var_screenWidth, height: var_screenHeight))
        var_titleLabel.text = title
        var_messageLabel.text = subTitle
        
        ka_setUI()
    }
    
    func ka_setUI() {
        frame = UIScreen.main.bounds
        backgroundColor = .clear

        addSubview(var_backgroundView)
        addSubview(var_contentView)
        var_contentView.addSubview(var_cornerView)
        var_cornerView.addSubview(var_titleLabel)
        var_cornerView.addSubview(var_messageLabel)
        var_cornerView.addSubview(var_closeBtn)
        var_cornerView.addSubview(var_switchBtn)
        
        var_cornerView.snp.makeConstraints { make in
            make.width.equalTo(kscale(280))
            make.height.equalTo(kscale(300))
            make.centerX.equalTo(self)
            make.bottom.equalTo(0)
        }
        var_cornerView.layer.cornerRadius = kscale(18)
        var_cornerView.layer.masksToBounds = true
        
        var_titleLabel.snp.makeConstraints { make in
            make.left.equalTo(var_cornerView).offset(kscale(20))
            make.right.equalTo(var_cornerView).offset(kscale(-20))
            make.top.equalTo(var_cornerView.snp.top).offset(kscale(20))
        }
        var_messageLabel.snp.makeConstraints { make in
            make.left.right.equalTo(var_titleLabel)
            make.top.equalTo(var_titleLabel.snp.bottom).offset(kscale(20))
        }
        
        var_switchBtn.snp.makeConstraints { make in
            make.left.right.equalTo(var_titleLabel)
            make.height.equalTo(kscale(40))
            make.centerX.equalTo(self)
            make.bottom.equalTo(var_closeBtn.snp.top).offset(kscale(-16))
        }
        
        var_closeBtn.snp.makeConstraints { make in
            make.left.right.equalTo(var_titleLabel)
            make.height.equalTo(var_switchBtn.snp.height)
            make.centerX.equalTo(self)
            make.bottom.equalTo(kscale(-16))
        }
        
        var_contentView.snp.makeConstraints { make in
            make.width.equalTo(var_cornerView.snp.width)
            make.height.equalTo(var_cornerView.snp.height)
            make.center.equalTo(self)
        }
    }
    
}
